package SwordForOffer.Problem28;

import java.util.Arrays;

public class Permutation {
	/*
	 * 输入一个字符串，打印出字符串中字符的所有排列
	 */
	public void permutation(String str) {
		if(str==null)
			return ;

		char[] output=str.toCharArray();

		int point=0;
		System.out.println(output);

		char temp1=output[point];
		output[point]=output[++point];
		output[point]=temp1;

		while(!String.valueOf(output).equals(str)) {
			System.out.println(output);
			if(point==output.length-1) {
				char temp=output[point];
				output[point]=output[0];
				output[0]=temp;
				point=0;
			}
			else {
				char temp=output[point];
				output[point]=output[++point];
				output[point]=temp;				
			}
		}
	}

	public void myPermutation(String string) {
		if (string == null || string.length() == 0){
			return;
		}
		if (string.length() == 1){
			System.out.println(string);
		}
		char[] chars = string.toCharArray();
		int startIndex = 0;
		myPermutation(chars,startIndex);
	}
	public void myPermutation(char[] chars,int startIndex){
		if (chars == null || chars.length == 0){
			return;
		}
		if (startIndex >= chars.length - 1){
			System.out.println(new String(chars));
			return;
		}
		for (int i = startIndex;i < chars.length ;i ++){
			char temp = chars[startIndex];
			chars[startIndex] = chars[i];
			chars[i] = temp;
			myPermutation(chars,startIndex + 1);
			temp = chars[startIndex];
			chars[startIndex] = chars[i];
			chars[i] = temp;
		}
	}
}
